package xps.and.uudaijia.userclient.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;

import java.util.ArrayList;
import java.util.List;

import xps.and.uudaijia.userclient.R;

public class PoiListAdapter extends BaseAdapter {
    List<PoiInfo> poi = new ArrayList<>();
    Context mContext;
    long lastTimeCall = 0;
    @Override
    public int getCount() {
        return poi.size();
    }

    @Override
    public Object getItem(int position) {
        return poi.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public PoiListAdapter(Context con) {
        mContext = con;
    }

    MyHolder myHolder = null;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            myHolder = new MyHolder();
            convertView = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_obtainaddressnearlist, null);
            convertView.setTag(myHolder);
            myHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            myHolder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        myHolder.tv_name.setText(poi.get(position).name);
        myHolder.tv_address.setText(poi.get(position).address);
        return convertView;
    }

    private final class MyHolder {
        TextView tv_name;
        TextView tv_address;
    }

    /**
     * 由于搜索地址列表返回之后 又把镜头移动到第一条结果 所以这个方法会被快速调两次
     * 所以这里判断一下用时间 忽略多余数据
     * */
    public void refreash(List<PoiInfo> poiss) {
        long millis = System.currentTimeMillis();
        if (millis - lastTimeCall > 500) {
            poi.clear();
            poi.addAll(poiss);
            notifyDataSetChanged();
            lastTimeCall = millis;
        } else {
            lastTimeCall = millis;
        }

    }

    public List<PoiInfo> getDataset() {
        return poi;
    }

}
